package API.Collection.BasicMethods.Contains;

import java.util.ArrayList;
import java.util.Collection;

public class Contains {
    public static void main(String[] args) {
        // 未重写equals方法的学生类
        StudentsNoOverrideEquals studentNo1 = new StudentsNoOverrideEquals("Jack", 18, "男");
        StudentsNoOverrideEquals studentNo2 = new StudentsNoOverrideEquals("Jack", 18, "男");
        Collection<StudentsNoOverrideEquals> studentNos = new ArrayList<>();
        studentNos.add(studentNo1);
        // 这样进行查询会返回false，因为contains底层是通过object中的equals进行判断，会直接比较地址值
        System.out.println(studentNos.contains(studentNo2));

        // 重写equals方法的学生类
        StudentsOverrideEquals student1 = new StudentsOverrideEquals("Jack", 18, "男");
        StudentsOverrideEquals student2 = new StudentsOverrideEquals("Jack", 18, "男");
        Collection<StudentsOverrideEquals> students = new ArrayList<>();
        students.add(student1);
        students.add(student2);
        // 由于相应类中已经重写了equals，所以返回true
        System.out.println(students.contains(student2));
    }
}
